package com.tianshi.pxx.controller.admin;


import com.tianshi.pxx.pojo.entity.Product;
import com.tianshi.pxx.pojo.entity.User;
import com.tianshi.pxx.pojo.vo.ProductVO;
import com.tianshi.pxx.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /*
    * 查询用户的所有订单
    * */
    @RequestMapping("/list")
    public ResponseEntity<List<Product>> list(HttpSession session) {
        //获取当前的用户信息
        User user = (User) session.getAttribute("currentUser");
        log.info("当前用户："+user.getUsername());
        log.info("用户id:{}",user.getId());
        List<Product> res=ordersService.list(user.getId());
        log.info("查询结果："+res);
        return ResponseEntity.ok(res);
    }

    /*
    * 根据商品名称查询订单
    * */
    @RequestMapping("/searchByName")//
    public ResponseEntity<List<Product>> searchByName(String productName, HttpSession session) {
        //获取当前的用户信息
        User user = (User) session.getAttribute("currentUser");
        log.info("当前用户："+user.getUsername());
        log.info("查询商品名称："+productName);

        List<Product> res=ordersService.searchByName(productName, user.getId());
        return ResponseEntity.ok(res);
    }

    /*
    *
    * 根据商品id删除订单信息
    * */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> delete(@PathVariable Integer id) {
        log.info("删除订单id："+id);
        ordersService.deleteById(id);
        return ResponseEntity.ok("删除成功");
    }

}



